<?php
namespace app\index\controller;

use app\common\entity\User;
use app\common\shop\CartModel;
use app\common\shop\ShopGoodsModel;
use app\common\shop\SpecListModel;
use app\common\shop\SpecModel;
use think\Db;
use think\Request;
use think\Controller;
// 购物车
class Shopcart extends Base {
    /**
     * [加入/修改购物车]
     * @param
     * @return
     */
    public function addCart( Request $request ){
        $uid = $this->userId;
        $goodsid = $request->post('goodsid');
        $goods = ShopGoodsModel::getGoods($goodsid);
        if(empty($goods)) return json(['code'=>1,'msg'=>'商品不存在']);
        if($goods['status'] == 0) return json(['code'=>1,'msg'=>'商品已下架']);
        $specList = (SpecListModel::getList($goodsid))->toArray();
        $speclistid = $request->post('speclistid');// 规格id
        if(!($speclist = SpecListModel::getInfo($speclistid)) ){
            return json(['code'=>1,'msg'=>'请选择正确规格']);
        }
        if(empty($speclistid) && $specList){
            return json(['code'=>1,'msg'=>'请选择规格']);
        }
        $number = $request->post('number');// 数量
        if($number<=0) return json(['code'=>1,'msg'=>'数量不能小于0']);
        $cart = CartModel::where(['goodsid'=>$goodsid,'speclistid'=>$speclistid])->find();
        $speclistTitle = '';
        if($speclist){
            $speclistTitle = SpecModel::getSpecTitle($speclist['key'],$goodsid);
        }
   
        $cart_data = [
            'uid'=>$uid,
            'goodsid'=>$goodsid,
            'speclistid'=>$speclistid,
            'number'=>$number,
            'speclistname'=>$speclistTitle,
        ];
        if($cart){
            CartModel::edit($cart_data,$cart['id']);
        }else{
            CartModel::add($cart_data);
        }
        return json(['code'=>0,'msg'=>'加入成功']);
    }

    /**
     * [购物车列表]
     * @param
     * @return
     */
    public function cartList( Request $request ){
        $uid = $this->userId;
        $page = $request->post('page');
        $limit = $request->post('limit');
        $list = CartModel::alias('c')
            ->field('c.id,c.goodsid,c.speclistid,c.number,g.title,g.price,g.status,g.unit,g.path,g.total')
            ->join('shop_goods g','g.id = c.goodsid','left')
            ->where(['c.uid'=>$uid])
            ->order('updatetime desc')
            ->page($page,$limit)
            ->select()
            ->toArray();
        foreach ($list as $key => $value) {
            $list[$key]['img'] = unserialize( $value['path'])[0];
            //$list[$key]['select'] = 0;
            if($value['speclistid']){
                $speclist_data = SpecListModel::getInfo($value['speclistid']);// 规格信息
                //if(empty($speclist_data)) return json(['code'=>1,'msg'=>'规格已不存在']);
                if(empty($speclist_data)){
                    $list[$key]['spec_title'] = '规格已不存在';
                }else{

                    // 规格标题
                    $title = SpecModel::getSpecTitle($speclist_data['key'],$value['goodsid']);
                    $list[$key]['price'] = $speclist_data['price'];//价格
                    $list[$key]['spec_title'] = $title;
                    $list[$key]['spec_title_arr'] = explode(';',$title);// 规格数组（可用于前端自定义样式）
                    $list[$key]['total'] = $speclist_data['total'];//库存
                    $list[$key]['specid'] = $speclist_data['id'] ;// 规格id
                }
            }
        }
        return json(['code' => 0, 'msg' => '请求成功','info'=>$list]);
    }

    /**
     * [删除购物车]
     * @param
     * @return
     */
    public function delCart( Request $request ){
        $uid = $this->userId;
        $cartid = $request->post('cartid');
        $cart = CartModel::where('id','in',$cartid)->where('uid',$uid)->select();
        if(empty($cart)) return json(['code'=>1,'msg'=>'已删除']);
        CartModel::where('id','in',$cartid)->where('uid',$uid)->delete();
        return json(['code'=>0,'msg'=>'删除成功']);
    }
}